package com.votesys.action;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.votesys.model.Votecontent;
import com.votesys.model.Votetopic;
import com.votesys.service.IContentService;
import com.votesys.service.ITopicService;
import com.votesys.util.IUtils;

public class TopicAction extends ActionSupport {
	private String topicname;
	private String topicdescription;
	private String logoname;
	private File logo;
	private String logoFileName;
	 private String imageContentType;
	private int showpattern;
	private int checkout;
	private int mincount;
	private int maxcount;
	private int flag;
	private int topicid;
	private int contentid;
	private InputStream inputStream;
	private ITopicService ITopicService;
	private IContentService IContentService;
	private IUtils IUtils;
	
	
	public String getTopicname() {
		return topicname;
	}

	public void setTopicname(String topicname) {
		this.topicname = topicname;
	}

	public String getTopicdescription() {
		return topicdescription;
	}

	public void setTopicdescription(String topicdescription) {
		this.topicdescription = topicdescription;
	}

	public String getLogoname() {
		return logoname;
	}

	public void setLogoname(String logoname) {
		this.logoname = logoname;
	}

	public File getLogo() {
		return logo;
	}

	public void setLogo(File logo) {
		this.logo = logo;
	}

	public String getLogoFileName() {
		return logoFileName;
	}

	public void setLogoFileName(String logoFileName) {
		this.logoFileName = logoFileName;
	}

	public String getImageContentType() {
		return imageContentType;
	}

	public void setImageContentType(String imageContentType) {
		this.imageContentType = imageContentType;
	}

	public int getShowpattern() {
		return showpattern;
	}

	public void setShowpattern(int showpattern) {
		this.showpattern = showpattern;
	}

	public int getCheckout() {
		return checkout;
	}

	public void setCheckout(int checkout) {
		this.checkout = checkout;
	}

	public int getMincount() {
		return mincount;
	}

	public void setMincount(int mincount) {
		this.mincount = mincount;
	}

	public int getMaxcount() {
		return maxcount;
	}

	public void setMaxcount(int maxcount) {
		this.maxcount = maxcount;
	}

	public int getFlag() {
		return flag;
	}

	public void setFlag(int flag) {
		this.flag = flag;
	}

	public int getTopicid() {
		return topicid;
	}

	public void setTopicid(int topicid) {
		this.topicid = topicid;
	}

	public int getContentid() {
		return contentid;
	}

	public void setContentid(int contentid) {
		this.contentid = contentid;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public ITopicService getITopicService() {
		return ITopicService;
	}

	public void setITopicService(ITopicService iTopicService) {
		ITopicService = iTopicService;
	}
	
	public IContentService getIContentService() {
		return IContentService;
	}

	public void setIContentService(IContentService iContentService) {
		IContentService = iContentService;
	}

	public IUtils getIUtils() {
		return IUtils;
	}

	public void setIUtils(IUtils iUtils) {
		IUtils = iUtils;
	}

	public String ShowTopic()throws Exception{
//		System.out.println("topicId:"+topicId);
		HttpServletRequest request = IUtils.getRequest();
		Votetopic VoteTopic = ITopicService.getVoteTopic(topicid);
		List VoteContentList = IContentService.getContentList(topicid);
		request.setAttribute("VoteContentList", VoteContentList);
		request.setAttribute("VoteTopic", VoteTopic);
		request.setAttribute("returnAction", "UpdateTopic");
		return "success";
	}
	
	public String NewTopic()throws Exception{
		HttpServletRequest request = IUtils.getRequest();
		request.setAttribute("returnAction", "AddTopic");
		return "success";
	}
	public String AddTopic()throws Exception{
		HttpServletRequest request = IUtils.getRequest();
		byte [] tempfile = IUtils.getFileInputStream(logo);
		if(tempfile!=null && topicname!=null && topicdescription!=null && logoname!=null){
			ITopicService.addNewTopic(topicname, topicdescription, logoname, tempfile, showpattern, checkout, mincount, maxcount, flag);
			//			BufferedInputStream bis=new BufferedInputStream(fis);
//			int c= bis.read();
//			while(c!=-1){
//				fos.write(c);
//				c = bis.read();
			
//			}	
		}else{
			System.out.println("attribute is empty");
		}
		return "success";
	}
	
	public String UpdateTopic()throws Exception{
		HttpServletRequest request =IUtils.getRequest();
		System.out.println(topicid);
		if(topicid!=0 && topicname!=null && topicdescription!=null && logoname!=null){
			Votetopic tempvotetopic = ITopicService.getVoteTopic(topicid);
//			tempvotetopic.setTopicId(topicid);
			tempvotetopic.setTopicName(topicname);
			tempvotetopic.setTopicDescription(topicdescription);
			tempvotetopic.setLogoName(logoname);
//			tempvotetopic.setLogo(tempfile);
			tempvotetopic.setCheckOut(checkout);
			tempvotetopic.setShowPattern(showpattern);
			tempvotetopic.setMinCount(mincount);
			tempvotetopic.setMaxCount(maxcount);
			tempvotetopic.setFlag(flag);
			ITopicService.updateTopic(tempvotetopic);
		}else{
			System.out.println("attribute is empty");
		}
		return "success";
	}
	
	public String deleteTopic()throws Exception{
		
		return "success";
	}
	
	public String ShowTopicList()throws Exception{
		HttpServletRequest request = IUtils.getRequest();
		List topicList = ITopicService.getTopicList();
		request.setAttribute("topicList", topicList);
		return "success";
	}
	
	public String RuntimeTopic()throws Exception{
		HttpServletRequest request = IUtils.getRequest();
		Votetopic RuntimeTopic = ITopicService.getRuntimeTopic();
		if(RuntimeTopic!=null){
			List RuntimeContentList = IContentService.getContentList(RuntimeTopic.getTopicId());
			request.setAttribute("RuntimeTopic", RuntimeTopic);
			request.setAttribute("RuntimeContentList", RuntimeContentList);
			return"success";
		}else{
			request.setAttribute("ErrorState", "RuntimeTopic empty.");
			return "error";
		}
	}
	
	public String SingleRuntimeContent()throws Exception{
		HttpServletRequest request = IUtils.getRequest();
		Votecontent VoteContent = IContentService.getContent(contentid);
		request.setAttribute("VoteContent", VoteContent);
		return "success";
	}
	
	public String execute()throws Exception{
	
	return "success";
	}
	
	public String getLogoInputStream()throws Exception{
		Votetopic RuntimeTopic = ITopicService.getRuntimeTopic();
		if(RuntimeTopic!=null){
			byte[] bytes = RuntimeTopic.getLogo();
			InputStream tempinputStream = new ByteArrayInputStream(bytes);
			this.setInputStream(tempinputStream);
			return "success";
		}else{
			return "error";
		}
		
	}
	
	/**
	 * change file type to byte array type
	 * @param file
	 * @return
	 * @throws IOException 
	 *//*
	public byte[] getFileInputStream(File tempfile) throws IOException{
		FileInputStream fis = null;
		int len = 0;
		byte[] buffer = new byte[1024];
		byte[] tempoutput = null;
		ByteArrayOutputStream fos = null;
		if(tempfile!=null){
//			System.out.println(logo.getPath());
			System.out.println(logoFileName);
			fis = new FileInputStream(tempfile);
			fos = new ByteArrayOutputStream();
			while((len=fis.read(buffer))!=-1){
				fos.write(buffer,0,len);
			}
			tempoutput = fos.toByteArray();
		}else{
			System.out.println("file is empty");
		}
		return tempoutput;
	}
	
	public HttpServletRequest getRequest(){
		ActionContext ac = ActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);
		return request;
	}
*/
}
